import pymysql as MySQLdb
import os
from flask import make_response
from flask import Flask, request, render_template, jsonify, Response, json
from nltk.stem.lancaster import LancasterStemmer
import re
app = Flask(__name__)
cited_dict = dict()
st = LancasterStemmer()

def xy_contains(an_id):
    if cited_dict[an_id] != -1:
        return 1
    return -1

@app.route('/SearchPapers/', methods = ['GET'])
def searchPapers():
    Keywords = str(request.values['target']).lower()
    page = int(request.values['page'])
    page_size = int(request.values['page_size'])
    Keywords = Keywords.split()
    pattern = r'[^(\x20-\x7F)]'
    result_dict = dict()
    for word in Keywords:
        stemmed = st.stem(word)
        stemmed = stemmed.replace(',', '').replace('.', '').replace('(', '')
        stemmed = stemmed.replace(')', '').replace('[', '').replace(']', '')
        stemmed = stemmed.replace('?', '').replace('/', '').replace('\\','')
        stemmed = stemmed.replace('\'','').replace('\"','')
        try:
            f = open("wordsTable/" + stemmed + ".json", 'r')
            tmplist = json.loads(f.read())
            f.close()
        except:
            continue
        for w in tmplist:
            if result_dict.get(w) == None:
                result_dict[w] = 0
            result_dict[w] += 1
    cited_dict.clear()
    conn = MySQLdb.connect(host='192.168.1.40',user='root',passwd='password',db='citeseerx',port=3306)
    cur = conn.cursor()
    for key in list(result_dict):
        cur.execute("select count from calculated where id = " + key)
        try:
            cited_dict[key] = cur.fetchone()[0]
        except:
            cited_dict[key] = -1
    result_list = list(result_dict)
    result_list.sort(key = cited_dict.get, reverse = True)
    result_list.sort(key = result_dict.get, reverse = True)
    result_list.sort(key = xy_contains, reverse = True)
    total_num = len(result_list)
    result_list = result_list[page*page_size: page*page_size + page_size]
    result_json = {'total_num': total_num, 'list': []}
    for result in result_list:
        cur.execute("select ctitle, cauth, cvenue, cyear from cluster where id = " + result)
        tmp = list(cur.fetchone())
        if tmp[0] == None:
            tmp[0] = ''
        if tmp[1] == None:
            tmp[1] = ''
        if tmp[2] == None:
            tmp[2] = ''
        if cited_dict[result] == -1:
            result_json['list'].append({
                'id': result,
                'title': re.sub(pattern, '', tmp[0]),
                'cited_count': cited_dict[result],
                'authors': re.sub(pattern, '', tmp[1]),
                'venue': re.sub(pattern, '', tmp[2]),
                'year': tmp[3],
                'selectable': 'No'
            })
        else:
            result_json['list'].append({
                'id': result,
                'title': re.sub(pattern, '', tmp[0]),
                'cited_count': cited_dict[result],
                'authors': re.sub(pattern, '', tmp[1]),
                'venue': re.sub(pattern, '', tmp[2]),
                'year': tmp[3],
                'selectable': 'Yes'
            })
    jsonp_callback = request.args.get('callback','')
    print(json.dumps(result_json))
    return Response( "%s(%s);" % (jsonp_callback, json.dumps(result_json)), mimetype = "text/javascript")

if __name__ == '__main__':
    app.run('0.0.0.0', port = 5003)